package com.wanwu.channel.core.tool;

import com.wanwu.channel.common.enums.AlarmTypeEnum;
import com.wanwu.monitor.api.dto.req.AddAlarmToPoolReqDto;
import com.wanwu.monitor.api.dto.req.vo.AlarmMsg;
import com.wanwu.monitor.api.timer.SendAlarmsToServerTimer;
import com.wanwu.monitor.api.util.AlarmPoolTool;
import com.wanwu.monitor.api.util.vo.AlarmMsgVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 钉钉告警池工具类
 * @author chen shunxing
 */
@Component
public class AlarmPoolUtil {

    //告警池服务的url
    @Value("${alarm.server.url}")
    private String serverUrl;

    //系统编码
    @Value("${alarm.system.code}")
    private String systemCode;

    //启动告警定时器
    @PostConstruct
    public void init(){
        SendAlarmsToServerTimer.startAlarmTimer(serverUrl);
    }

    /**
     * 添加需要聚合的告警
     * @param alarmType
     * @param msg
     */
    public void addGatherAlarm(String dingKey, AlarmTypeEnum alarmType, String msg){
        AlarmMsg alarmMsg = new AlarmMsg(null, msg);
        AddAlarmToPoolReqDto reqDto = new AddAlarmToPoolReqDto.Builder()
                .setSystemCode(systemCode)
                .setDingKey(dingKey)
                .setAlarmKey(alarmType.name())
                .setTitle(alarmType.getDesc())
                .setAlarmMsg(alarmMsg)
                .build();
        AlarmPoolTool.addGatherAlarm(reqDto);
    }

    public void addGatherAlarm(String dingKey, AlarmTypeEnum alarmType, String msg, String... args){
        AlarmMsg alarmMsg = new AlarmMsg(null, msg);
        alarmMsg.setBody(msg);
        AddAlarmToPoolReqDto reqDto = new AddAlarmToPoolReqDto.Builder()
                .setSystemCode(systemCode)
                .setDingKey(dingKey)
                .setAlarmKey(alarmType.name())
                .setTitle(alarmType.getDesc())
                .setAlarmMsg(alarmMsg)
                .setAlarmArgs(args)
                .build();
        AlarmPoolTool.addGatherAlarm(reqDto);
    }

    public void addGatherAlarm(String dingKey, AlarmTypeEnum alarmType, AlarmMsgVo alarmMsgVo){
        AddAlarmToPoolReqDto reqDto = new AddAlarmToPoolReqDto.Builder()
                .setSystemCode(systemCode)
                .setDingKey(dingKey)
                .setAlarmKey(alarmType.name())
                .setTitle(alarmType.getDesc())
                .setAlarmMsg(alarmMsgVo.getAlarmMsg())
                .setAlarmArgs(alarmMsgVo.getArgs())
                .build();
        AlarmPoolTool.addGatherAlarm(reqDto);
    }


    /**添加非聚合的告警
     *
     * @param dingKey
     * @param alarmType
     * @param msg
     */
    public void addCommonAlarm(String dingKey, AlarmTypeEnum alarmType, String msg){
        AlarmMsg alarmMsg = new AlarmMsg(null, msg);
        AddAlarmToPoolReqDto reqDto = new AddAlarmToPoolReqDto.Builder()
                .setSystemCode(systemCode)
                .setDingKey(dingKey)
                .setAlarmKey(alarmType.name())
                .setTitle(alarmType.getDesc())
                .setAlarmMsg(alarmMsg)
                .build();
        AlarmPoolTool.addCommonAlarm(reqDto);
    }
    public void addCommonAlarm(String dingKey, AlarmTypeEnum alarmType, String msg, String... args){
        AlarmMsg alarmMsg = new AlarmMsg(null, msg);
        AddAlarmToPoolReqDto reqDto = new AddAlarmToPoolReqDto.Builder()
                .setSystemCode(systemCode)
                .setDingKey(dingKey)
                .setAlarmKey(alarmType.name())
                .setTitle(alarmType.getDesc())
                .setAlarmMsg(alarmMsg)
                .setAlarmArgs(args)
                .build();
        AlarmPoolTool.addCommonAlarm(reqDto);
    }
    public void addCommonAlarm(String dingKey, AlarmTypeEnum alarmType, AlarmMsgVo alarmMsgVo){
        AddAlarmToPoolReqDto reqDto = new AddAlarmToPoolReqDto.Builder()
                .setSystemCode(systemCode)
                .setDingKey(dingKey)
                .setAlarmKey(alarmType.name())
                .setTitle(alarmType.getDesc())
                .setAlarmMsg(alarmMsgVo.getAlarmMsg())
                .setAlarmArgs(alarmMsgVo.getArgs())
                .build();
        AlarmPoolTool.addCommonAlarm(reqDto);
    }

    /**
     * 立即发送告警到告警池服务上，不需要进入本地缓冲池！！！
     * @param dingKey
     * @param alarmType
     * @param msg
     */
    public void sendAlarmAtOnce(String dingKey, AlarmTypeEnum alarmType, String msg){
        AlarmMsg alarmMsg = new AlarmMsg(null, msg);
        AddAlarmToPoolReqDto reqDto = new AddAlarmToPoolReqDto.Builder()
                .setSystemCode(systemCode)
                .setDingKey(dingKey)
                .setAlarmKey(alarmType.name())
                .setTitle(alarmType.getDesc())
                .setAlarmMsg(alarmMsg)
                .build();
        AlarmPoolTool.sendAlarmAtOnce(reqDto, serverUrl);
    }
    public void sendAlarmAtOnce(String dingKey, AlarmTypeEnum alarmType, String msg, String... args){
        AlarmMsg alarmMsg = new AlarmMsg(null, msg);
        AddAlarmToPoolReqDto reqDto = new AddAlarmToPoolReqDto.Builder()
                .setSystemCode(systemCode)
                .setDingKey(dingKey)
                .setAlarmKey(alarmType.name())
                .setTitle(alarmType.getDesc())
                .setAlarmMsg(alarmMsg)
                .setAlarmArgs(args)
                .build();
        AlarmPoolTool.sendAlarmAtOnce(reqDto, serverUrl);
    }
    public void sendAlarmAtOnce(String dingKey, AlarmTypeEnum alarmType, AlarmMsgVo alarmMsgVo){
        AddAlarmToPoolReqDto reqDto = new AddAlarmToPoolReqDto.Builder()
                .setSystemCode(systemCode)
                .setDingKey(dingKey)
                .setAlarmKey(alarmType.name())
                .setTitle(alarmType.getDesc())
                .setAlarmMsg(alarmMsgVo.getAlarmMsg())
                .setAlarmArgs(alarmMsgVo.getArgs())
                .build();
        AlarmPoolTool.sendAlarmAtOnce(reqDto, serverUrl);
    }

}
